package com.jp.fm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.junit.Test;

import com.jp.fm.dao.AccountDao;
import com.jp.fm.dao.CategoryDao;
import com.jp.fm.data.Account;
import com.jp.fm.data.Account.AccountBalance;
import com.jp.fm.service.CategoryService;

public class HomeFinanceRunnerTest {

	@Test
	public final void testRefreshAccountFromSourceFailWithTeardownSuccess() {

		Account acct = new Account(null, "Visa Card", null, null, null,
				new AccountBalance(null, Float.valueOf("10"), new Date()), 0);

		HomeFinanceRunner.refreshAccountFromSource(acct, new CategoryService(),
				new CategoryDao(), new AccountDao());
	}

	@Test
	public final void testRefreshAccountFromSourceSuccess() {

		Account acct = new Account(null, "Checking Account", null, null, null,
				null, 0);

		HomeFinanceRunner.refreshAccountFromSource(acct, new CategoryService(),
				new CategoryDao(), new AccountDao());
	}

	@Test
	public final void testRemoveEnclosingQuotes() {

		String ret = HomeFinanceRunner.removeEnclosingQuotes("\"test\"");
		assertEquals("test", ret);
	}

	@Test
	public final void testDaysBetween() {

		Calendar d1 = new GregorianCalendar();
		Calendar d2 = new GregorianCalendar();
		d2.add(Calendar.DAY_OF_MONTH, 1);

		int ret = HomeFinanceRunner.daysBetween(d1.getTime(), d2.getTime());

		assertEquals(1, ret);
	}

	@Test
	public final void testHoursBetween() {

		Calendar d1 = new GregorianCalendar();
		Calendar d2 = new GregorianCalendar();
		d2.add(Calendar.HOUR, 1);

		int ret = HomeFinanceRunner.hoursBetween(d1.getTime(), d2.getTime());

		assertEquals(1, ret);
	}

	@Test
	public final void testMinutesBetween() {

		Calendar d1 = new GregorianCalendar();
		Calendar d2 = new GregorianCalendar();
		d2.add(Calendar.MINUTE, 1);

		int ret = HomeFinanceRunner.minutesBetween(d1.getTime(), d2.getTime());

		assertEquals(1, ret);
	}

	@Test
	public final void testGetLastUpdateText() {

		Calendar c = new GregorianCalendar();
		c.add(Calendar.MINUTE, -10);
		Timestamp ts = new Timestamp(c.getTime().getTime());

		String ret = HomeFinanceRunner.getLastUpdateText(ts);
		assertNotNull(ret);
		assertEquals("10 minutes", ret);
	}
}
